
<?php
header('Content-type:text/html;charset=UTF-8');

class test{
	const HOST = "localhost";
	const NAME = "root";
	const PASS = "";
	const DBNAME = "test";
	public static function Con(){
		$mysqli= new mysqli(self::HOST,self::NAME,self::PASS,self::DBNAME);
		mysqli_set_charset($mysqli,'utf8');
		if($mysqli->connect_error!= 0){
			$mysqli->set_charset("utf8");
			die("连接失败!").$mysqli->connect_error;
		}else{
			return $mysqli;
		}
	}
}
class writing{
	const HOST = "localhost";
	const NAME = "root";
	const PASS = "";
	const DBNAME = "writing";
	public static function Con(){
		$mysqli= new mysqli(self::HOST,self::NAME,self::PASS,self::DBNAME);
		mysqli_set_charset($mysqli,'utf8');
		if($mysqli->connect_error!= 0){
			$mysqli->set_charset("utf8");
			die("连接失败!").$mysqli->connect_error;
		}else{
			return $mysqli;
		}
	}
}

class my{
//获取中国时间函数
    public function GetChinaTime($format = "Y-m-d H:i:s"){
		$timezone_out = date_default_timezone_get();
		date_default_timezone_set('Asia/Shanghai');
		$chinaTime = date($format);
		date_default_timezone_set($timezone_out);
		return $chinaTime;
	}
//跟踪最后一次文章变更函数
    public function NumAdd(){
		$mysqli = new writing;
		$mysqli = $mysqli -> Con();
		$sql = "select * from `write`";
		$res = $mysqli->query($sql);
		$num = 0;
		foreach($res as $var){
			if($var['num']>=$num){
				$num = $var['num'];
			}
		}
		$num +=1;
		return $num;
	}
//获取最后一次变更文章的对象函数
    public function LastArticle(){
		$mysqli = new writing;
		$mysqli = $mysqli -> Con();
		$sql = "select * from `write`";
		$res = $mysqli->query($sql);
		$num = 0;
		foreach($res as $var){
			if($var['author']==$_SESSION['ath']){
				if($var['num']>=$num){
					$num = $var['num'];
				}
			}
		}
		$sql = "select * from `write` where num = '{$num}'";
		$res = $mysqli->query($sql);
		return $res;
	}
//获取对应id文章的对象函数
    public function SelectId($id){
		$mysqli = new writing;
		$mysqli = $mysqli -> Con();
		$sql = "select * from `write` where id= '{$id}'";
		$res=$mysqli->query($sql);
		return $res;
	}
//显示个人头像函数
    public function ShowPhoto($name){
		$mysqli = new test;
		$mysqli = $mysqli -> Con();
		$sqlstr = "select * from user where username= '{$name}'"; 
		$query = $mysqli->query($sqlstr);
		foreach ($query as $var){	
			$pht=base64_encode($var['photo']);
			return '<img src="data:'.$var['type'].';base64,'.$pht.'" alt="无法显示" width="150" height="150"/> ';  }
		}
//限制文本输出数量函数
    public function Limit($str,$slen){
		$str=$str;
		$strlen=mb_strlen($str);
		if($strlen<=$slen){
			return $str;
		}else{
			$str=mb_substr($str,0,$slen,'utf-8');
			return $str.'……';
		}
	}	
//超长文本页码输出函数
public function Page($str,$pages,$slen){
	$strlen=mb_strlen($str);
	$str=mb_substr($str,($pages-1)*$slen,$strlen,'utf-8');
	$strlen=mb_strlen($str);
	if($strlen<=$slen){
		return $str;
	}else{
		$str=mb_substr($str,0,$slen,'utf-8');
		return $str;
	}
}	
}

		
?>